package com.guli.edu.controller;


import com.guli.base.exception.GuliException;
import com.guli.edu.entity.Chapter;
import com.guli.edu.entity.vo.ChapterVo;
import com.guli.edu.service.ChapterService;
import com.guli.edu.service.VideoService;
import com.guli.utils.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ZhaoZhenyu
 * @since 2022-09-26
 */

//@CrossOrigin
@Api(description="课程章节管理")
@RestController
@RequestMapping("/edu/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private VideoService videoService;

    // 得到嵌套视频的章节集合
    @GetMapping("/nestedListChapter/{courseId}")
    public R nestedListChapter(@PathVariable String courseId){
        List<ChapterVo> chapterVoList = chapterService.nestedListChapterByCourseId(courseId);
        return R.ok().data("items",chapterVoList);
    }

    // 新增章节
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody Chapter chapter){
        boolean flag = chapterService.save(chapter);
        if (flag){
            return R.ok();
        }else {
            return R.error().message("添加章节失败");
        }
    }

    // 根据id查询
    @GetMapping("/getChapter/{id}")
    public R getChapterById(@PathVariable String id){
        Chapter chapter = chapterService.getById(id);
        if (!ObjectUtils.isEmpty(chapter)){
            return R.ok().data("item",chapter);
        }else {
            return R.error().message("查询章节失败");
        }
    }

    // 更新
    @PutMapping("/updateChapter/{id}")
    public R updateChapterById(@RequestBody Chapter chapter,@PathVariable String id){
        chapter.setId(id);
        boolean flag = chapterService.updateById(chapter);
        if (flag){
            return R.ok();
        }else {
            return R.error().message("更新章节失败");
        }
    }

    // 提醒删除
    @DeleteMapping("/deleteChapter/{id}")
    public R deleteChapterById(@PathVariable String id){

        // TODO 查该章节对应的 video表是否有内容 有的话远程在videoController调用删除
        // video表里面的chapterId对应传入id
        int count = videoService.getCount(id);
        if (count != 0) throw new GuliException(20001, "该分章节下存在视频课程，请先删除小结和视频");
        boolean flag = chapterService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error().message("删除章节失败");
        }
    }

    // 强制删除
    @DeleteMapping("/forceDeleteChapter/{id}")
    public R forceDeleteChapter(@PathVariable String id){

        // TODO 查该章节对应的 video表是否有内容 有的话远程在videoController调用删除
        // video表里面的chapterId对应传入id
        int count = videoService.DeleteChapterBatchByChapterId(id);
        if (count == 0) throw new GuliException(20001, "删除小结或视频失败");
        boolean flag = chapterService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error().message("删除章节失败");
        }
    }


}

